package weavingdraft;
import java.awt.GridLayout;

import javax.swing.JPanel;

/**
 * Creates the ThreadGrid panel allowing users to interact with it. 
 * @author Justin Wong
 *
 */
public class ThreadGrid extends JPanel{
	/**
	 * The WeavingDraft it refers to. 
	 */
	private WeavingDraft my_draft;
	/**
	 * The length of the grid. 
	 */
	private int long_side;
	/**
	 * The height of the grid. 
	 */
	private int short_side;
	/**
	 * The array holding ThreadingSquare objects being allowed for changes. 
	 */
	private ThreadingSquare[] threading_panels;
	
	/**
	 * Creates ThreadGrid object allowing for users to interact with it. 
	 * @param the_draft WeavingDraft object this grid refers to. 
	 */
	public ThreadGrid(WeavingDraft the_draft){
		my_draft = the_draft;
		long_side = my_draft.getLongside();
		short_side = my_draft.getShortside();
		threading_panels = new ThreadingSquare[long_side];
		start();
	}
	/**
	 * Get the object of the ThreadingSquare. 
	 * @param column the column number. 
	 * @return the ThreadingSquare this object refers to. 
	 */
	public ThreadingSquare getSquare(int column){
		return threading_panels[column];
	}
	/**
	 * Sets a square to the object ThreadingSquare to be setting. 
	 * 
	 * @param square the ThreadingSquare this object refers to. 
	 * @param column the column number to set to. 
	 */
	public void setSquare(ThreadingSquare square, int column){
		threading_panels[column] = square;
	}
	/**
	 * Starts the object and creates all the squares needed in the grid. 
	 */
	private void start(){
		this.setLayout(new GridLayout(short_side, long_side));
		createSquares();
		this.setVisible(true);
	}
	/**
	 * Create the squares of the ThreadGrid. 
	 */
	private void createSquares(){
		for(int i = 0; i < short_side; i++){
			for(int j = 0; j < long_side; j++){
				ThreadingSquare square = new ThreadingSquare(i, j, my_draft, this);
				this.add(square);
			}
		}
	}
}
